package cn.cmkj.auction.data.http;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import cn.cmkj.auction.app.Urls;

/**
 * Created by cunguoyao on 2017/9/11.
 */

public class Shuhua implements Serializable {

    private long id;
    private long productId;//货品id
    private String name;
    private BigDecimal oldPrice;
    private BigDecimal salePrice;
    private int readNum;
    private int commentNum;
    private ShuhuaImage defaultImage;
    private String bn;
    private String brief;
    private String typeName;//分类
    private int quantity;//订单里要用到的个数

    //以下是书画详情中 要用到的
    private List<ShuhuaImage> images;
    private List<ShuhuaProps> props;

    //和用户相关的属性
    private boolean isFav;
    private boolean isCart;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getProductId() {
        return productId;
    }

    public void setProductId(long productId) {
        this.productId = productId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getOldPrice() {
        return oldPrice;
    }

    public void setOldPrice(BigDecimal oldPrice) {
        this.oldPrice = oldPrice;
    }

    public BigDecimal getSalePrice() {
        return salePrice;
    }

    public void setSalePrice(BigDecimal salePrice) {
        this.salePrice = salePrice;
    }

    public int getReadNum() {
        return readNum;
    }

    public void setReadNum(int readNum) {
        this.readNum = readNum;
    }

    public int getCommentNum() {
        return commentNum;
    }

    public void setCommentNum(int commentNum) {
        this.commentNum = commentNum;
    }

    public ShuhuaImage getDefaultImage() {
        return defaultImage;
    }

    public void setDefaultImage(ShuhuaImage defaultImage) {
        this.defaultImage = defaultImage;
    }

    public void setImages(List<ShuhuaImage> images) {
        this.images = images;
    }

    public List<ShuhuaImage> getImages() {
        return images;
    }

    public List<ShuhuaProps> getProps() {
        return props;
    }

    public void setProps(List<ShuhuaProps> props) {
        this.props = props;
    }

    public String getBn() {
        return bn;
    }

    public void setBn(String bn) {
        this.bn = bn;
    }

    public String getBrief() {
        return brief;
    }

    public void setBrief(String brief) {
        this.brief = brief;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public boolean isFav() {
        return isFav;
    }

    public void setFav(boolean fav) {
        isFav = fav;
    }

    public boolean isCart() {
        return isCart;
    }

    public void setCart(boolean cart) {
        isCart = cart;
    }

    public static Shuhua parseFromJson(JSONObject json) {
        Shuhua bean = new Shuhua();
        bean.setId(json.optLong("goods_id", 0));
        bean.setProductId(json.optLong("product_id", 0));
        bean.setName(json.optString("name", ""));
        bean.setBrief(json.optString("brief", ""));
        bean.setBn(json.optString("bn", ""));
        double oldPrice = json.optDouble("mktprice", 0);
        double salePrice = json.optDouble("price", 0);
        bean.setOldPrice(BigDecimal.valueOf(oldPrice));
        bean.setSalePrice(BigDecimal.valueOf(salePrice));
        bean.setReadNum(json.optInt("read_count", 0));
        bean.setCommentNum(json.optInt("comments_count", 0));
        bean.setDefaultImage(ShuhuaImage.parseFromJson(json.optJSONObject("image"), false));

        return bean;
    }

    public static List<Shuhua> parseJsonArray(JSONArray array) {
        List<Shuhua> list = new ArrayList<>();
        if(array != null && array.length() > 0){
            for(int i=0;i<array.length();i++){
                list.add(parseFromJson(array.optJSONObject(i)));
            }
        }
        return list;
    }

    public static Shuhua parseFromJsonForDetail(JSONObject json) {
        Shuhua bean = new Shuhua();
        bean.setId(json.optLong("goods_id", 0));
        bean.setName(json.optString("title", ""));
        bean.setBrief(json.optString("brief", ""));
        bean.setProductId(json.optLong("product_id", 0));
        bean.setBn(json.optString("product_bn", ""));
        //double oldPrice = json.optDouble("mktprice", 0);
        double salePrice = json.optDouble("price", 0);
        //bean.setOldPrice(BigDecimal.valueOf(oldPrice));
        bean.setSalePrice(BigDecimal.valueOf(salePrice));
        bean.setReadNum(json.optInt("read_count", 0));
        bean.setCommentNum(json.optInt("comments_count", 0));
        bean.setDefaultImage(ShuhuaImage.parseFromJson(json.optJSONObject("image_default_id"), true));
        bean.setImages(ShuhuaImage.parseJsonArray(json.optJSONArray("images"), true));
        bean.setProps(ShuhuaProps.parseJsonArray(json.optJSONArray("props")));
        bean.setQuantity(json.optInt("quantity", 1));
        bean.setTypeName(json.optString("type_name", ""));
        return bean;
    }

    public static Shuhua parseFromJsonForOrder(JSONObject json) {
        Shuhua bean = new Shuhua();
        bean.setId(json.optLong("goods_id", 0));
        bean.setName(json.optString("goods_name", ""));
        bean.setBrief(json.optString("brief", ""));
        bean.setProductId(json.optLong("product_id", 0));
        bean.setBn(json.optString("product_bn", ""));
        //double oldPrice = json.optDouble("mktprice", 0);
        double salePrice = json.optDouble("price", 0);
        //bean.setOldPrice(BigDecimal.valueOf(oldPrice));
        bean.setSalePrice(BigDecimal.valueOf(salePrice));
        bean.setReadNum(json.optInt("read_count", 0));
        bean.setCommentNum(json.optInt("comments_count", 0));
        ShuhuaImage image = new ShuhuaImage();
        String pic = json.optString("pic", "");
        if(pic.contains(Urls.web)) {
            image.l_url = pic;
            image.m_url = pic;
            image.s_url = pic;
        }else {
            image.l_url = Urls.web + "/" + pic;
            image.m_url = Urls.web + "/" + pic;
            image.s_url = Urls.web + "/" + pic;
        }
        bean.setDefaultImage(image);
        bean.setQuantity(json.optInt("quantity", 1));
        return bean;
    }

    public static List<Shuhua> parseJsonArrayForFav(JSONArray array) {
        List<Shuhua> list = new ArrayList<>();
        if(array != null && array.length() > 0){
            for(int i=0;i<array.length();i++){
                list.add(parseFromJsonForFav(array.optJSONObject(i)));
            }
        }
        return list;
    }

    public static Shuhua parseFromJsonForFav(JSONObject json) {
        Shuhua bean = new Shuhua();
        bean.setId(json.optLong("goods_id", 0));
        bean.setProductId(json.optLong("product_id", 0));
        bean.setName(json.optString("goods_name", ""));
        double salePrice = json.optDouble("goods_price", 0);
        bean.setSalePrice(BigDecimal.valueOf(salePrice));
        bean.setDefaultImage(ShuhuaImage.parseFromJson(json.optJSONObject("goods_pic"), false));

        return bean;
    }
}
